package smallchange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SmallChangeSys {

    public static void main(String[] args) {
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        String key = "";
        String detail = "-------------零钱通明细-------------";
        double money = 0;
        double balance = 0;
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String note = "";

        do {
            System.out.println("\n=============零钱通菜单==============");
            System.out.println("\t\t\t1 零钱通明细");
            System.out.println("\t\t\t2 收益入账");
            System.out.println("\t\t\t3 消费");
            System.out.println("\t\t\t4 退出");
            System.out.print("请选择 1-4：");
            key = scanner.next();

            switch (key) {
                case "1":
                    System.out.println(detail);;
                    break;
                case "2":
                    System.out.print("收益入账金额：");
                    money = scanner.nextDouble();
                    if(money <= 0) {
                        System.out.println("收益入账金额必须大于0");
                        break;
                    }
                    balance += money;
                    date = new Date();//获取当前日期
                    //拼接收益信息到detail
                    detail += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + "余额：" + balance;
                    break;
                case "3":
                    System.out.print("消费金额：");
                    money = scanner.nextDouble();
                    if(money <= 0 || money > balance) {
                        System.out.println("你的消费金额需在0-" + balance);
                        break;
                    }
                    System.out.print("消费说明：");
                    note = scanner.next();
                    balance -= money;
                    date = new Date();//获取当前日期
                    //拼接消费信息到detail
                    detail += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + "余额：" + balance;
                    break;
                case "4":
                    String choice = "";
                    while(true) { // 要求用户必须输入 y/n ，否则一直循环
                        System.out.println("你确定要退出吗？ y/n");
                        choice = scanner.next();
                        if("y".equals(choice) || "n".equals(choice)) {
                            break;
                        }
                    }
                    if(choice.equals("y")) {
                        loop = false;
                    }
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
            }

        } while(loop);

        System.out.println("你已退出零钱通");
    }
}
